import Mock from 'mockjs';
import qs from 'qs'
export function login(req){
    const { account, password } = JSON.parse(req.body)
    if(account === 'lisi' && password === '123456'){
        return {
            code: 2, message: "登录成功",
            data: {
                "user": { "account": "lisi", "password": "12345"},
                "token": "20191011133051"
            }
        }
    }else {
        return {
            code: 0, message: "用户名或者密码错误"
        }
    }
}

export function registry(req) {
    return {
        code: 2, message: "注册成功",
        data: null
    }
}


let articles = []
for(let i = 0; i < 100; i ++) {
    articles.push(Mock.mock({
        "id": i,
        "title": "@ctitle(15, 50)",
        "content": "@cparagraph(2, 5)",
        "username": "admin",
        "date": "@datetime"
    }))
}

export function getArticles(req) {
    console.log(qs.parse(req.url))
    const obj = qs.parse(req.url)
    const key = Object.keys(obj)
    var [index, size, total] = [+obj[key[0]], 10, articles.length]
    var len = total / size
    var totalPages = len - parseInt(len) > 0 ? parseInt(len) + 1 : len
    var newArticles = articles.slice(index * size, (index + 1) * size)
    return {
        "total": total,
        "list": newArticles,
        "pageNum": index,
        "pageSize": size,
        "size": 3,
        "startRow": 1,
        "endRow": 3,
        "pages": 1,
        "prePage": 0,
        "nextPage": 0,
        "isFirstPage": true,
        "isLastPage": true,
        "hasPreviousPage": false,
        "hasNextPage": false,
        "navigatePages": 8,
        "navigatepageNums": [1],
        "navigateFirstPage": 1,
        "navigateLastPage": 1
    }
}